﻿using System;

using System.Collections.Generic;
using System.Text;
using System.IO;

namespace gpsmid_wince.util {
	class JavaDataInputStream {
		private BinaryReader rd;
		//private MemoryStream buffer;
		//private BinaryReader brd;

		public JavaDataInputStream(Stream ss) {
			if(!ss.CanRead) throw new InvalidOperationException("This stream is not for reading!");
			rd = new BinaryReader( ss, Encoding.UTF8 );	
			//buffer = new MemoryStream(10);
			//brd = new BinaryReader(buffer);
		}

		public int readInt() {
			byte[] bb = new byte[4];
			bb[3] = rd.ReadByte();
			bb[2] = rd.ReadByte();
			bb[1] = rd.ReadByte();
			bb[0] = rd.ReadByte();
            return BitConverter.ToInt32(bb, 0);
		}
		public short readShort() {
			byte t1 = rd.ReadByte(), t2 = rd.ReadByte();
            return (short)((int)t2 | (t1 << 8));
		}
		public long readLong() {
			byte[] bb = new byte[8];
			for(int i=7; i>=0; i--)
				bb[i] = rd.ReadByte();
            return BitConverter.ToInt64(bb, 0);
		}

		public sbyte readByte() {
			return rd.ReadSByte();
		}

		public string readUTF() {
			//float ff = readFloat();
			int len = readShort();
            StringBuilder sb = new StringBuilder(len);
            for (int i = 0; i < len; i++) sb.Append(rd.ReadChar() );
            return sb.ToString();
		}

		public char readChar() {
			return rd.ReadChar();
		}

		public float readFloat() {
			byte[] bb = new byte[4];
			bb[3] = rd.ReadByte();
			bb[2] = rd.ReadByte();
			bb[1] = rd.ReadByte();
			bb[0] = rd.ReadByte();
            return BitConverter.ToSingle(bb, 0);
		}

		public double readDouble() {
			byte[] bb = new byte[8];
			for(int i=7; i>=0; i--)
				bb[i] = rd.ReadByte();
            return BitConverter.ToDouble(bb, 0);
		}

		public bool readBoolean() {
			return rd.ReadBoolean();
		}

		public void close() {rd.Close();}
	}
}
